package com.totcy.atom.view.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.totcy.atom.R;
import com.totcy.atom.adapter.MMVideoAdapter;
import com.totcy.atom.bean.MMVideoBean;
import com.totcy.atom.bean.MMVideoResp;
import com.totcy.atom.presenter.MMVideoPresenter;
import com.totcy.atom.presenter.contract.MMVideoContract;
import com.totcy.baselibrary.adapter.loadmore.OnLoadMoreListener;
import com.totcy.baselibrary.utils.StatusBarUtil;
import com.totcy.baselibrary.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.jzvd.JZMediaManager;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdMgr;

public class WidgetFragment extends BaseFragment<MMVideoContract.View, MMVideoPresenter> implements OnLoadMoreListener, OnRefreshListener, MMVideoContract.View {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView rvVideo;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;

    private MMVideoAdapter adapter;
    private List<MMVideoBean> videoBeans = new ArrayList<>();
    private int pageNum = 1;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_widget;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        StatusBarUtil.setPaddingSmart(getActivity(), toolbar);

        LinearLayoutManager lm = new LinearLayoutManager(getActivity());
        lm.setOrientation(LinearLayoutManager.VERTICAL);
        rvVideo.setLayoutManager(lm);
        adapter = new MMVideoAdapter(getActivity(), videoBeans);
        rvVideo.setAdapter(adapter);

        adapter.setOnLoadMoreListener(rvVideo, this);
        //设置回弹动画插值器 减速效果
        refresh.setReboundInterpolator(new DecelerateInterpolator());
        refresh.setOnRefreshListener(this);
        //自动刷新
        refresh.autoRefresh(200);
        rvVideo.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {

            }

            @Override
            public void onChildViewDetachedFromWindow(View view) {
                Jzvd jzvd = view.findViewById(R.id.videoplayer);
                if (jzvd != null && jzvd.jzDataSource.containsTheUrl(JZMediaManager.getCurrentUrl())) {
                    Jzvd currentJzvd = JzvdMgr.getCurrentJzvd();
                    if (currentJzvd != null && currentJzvd.currentScreen != Jzvd.SCREEN_WINDOW_FULLSCREEN) {
                        Jzvd.releaseAllVideos();
                    }
                }
            }
        });
    }


    @Override
    public void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        pageNum = 1;
        getPresenter().getVideoList(pageNum);
    }

    @Override
    public void onLoadMore() {
        pageNum++;
        getPresenter().getVideoList(pageNum);
    }

    @Override
    public void onClickLoadMore() {
        getPresenter().getVideoList(pageNum);
    }

    @Override
    public void onVideoList(MMVideoResp videos) {
        if (pageNum == 1) {
            videoBeans.clear();
            refresh.finishRefresh(true);
        }
        videoBeans.addAll(videos.getLink_list());
        adapter.notifyDataSetChanged();
        adapter.setLoadMoreFinish(videoBeans.size() < videos.getLink_total());

    }

    @Override
    public void onFail(String message) {
        ToastUtil.showToast(getActivity(),message);
        adapter.setLoadFail();
        refresh.finishRefresh(false);
    }
}
